package com.bridge.common.utils;

import android.os.Handler;
import android.widget.Toast;

import com.bridge.common.base.BaseActivity;
import com.bridge.common.base.BaseAppLication;

public class ToastshowUtils {
    /** 对toast的简易封装。线程安全，可以在非UI线程调用。 */
    public static void showToastSafe(final String str) {
        if (isRunInMainThread()) {
            showToast(str);
        } else {
            post(new Runnable() {
                @Override
                public void run() {
                    showToast(str);
                }
            });
        }
    }
    /** 获取主线程的handler */
    public static Handler getHandler() {
        return BaseAppLication.getMainThreadHandler();
    }
    private static void showToast(String str) {
        BaseActivity frontActivity = BaseActivity.getForegroundActivity();
        if (frontActivity != null) {
            Toast.makeText(frontActivity, str, Toast.LENGTH_LONG).show();
        }
    }
    /** 在主线程执行runnable */
    public static boolean post(Runnable runnable) {
        return getHandler().post(runnable);
    }
    // 判断当前的线程是不是在主线程
    public static boolean isRunInMainThread() {
        return android.os.Process.myTid() == getMainThreadId();
    }
    public static long getMainThreadId() {
        return BaseAppLication.getMainThreadId();
    }

}